#define _CRT_SECURE_NO_WARNINGS

#include "All.h"

void QInit(Que* pq)
{
	assert(pq);

	pq->head = NULL;
	pq->tail = NULL;
	pq->size = 0;
}

QNode* BuyQNode(QDataType x)
{
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	assert(newnode);
	newnode->data = x;
	newnode->next = NULL;

	return newnode;
}

void QPush(Que* pq, QDataType x)
{
	assert(pq);

	QNode* newnode = BuyQNode(x);
	if (pq->head == NULL)
	{
		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
	pq->size++;
}

void QPop(Que* pq)
{
	assert(pq && !QEmpty(pq));

	if (pq->head == pq->tail)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		QNode* cur = pq->head;
		pq->head = pq->head->next;
		free(cur);
	}
	pq->size--;
}

bool QEmpty(Que* pq)
{
	return pq->size == 0;
}

QDataType QFront(Que* pq)
{
	assert(pq && !QEmpty(pq));

	return pq->head->data;
}

QDataType QBack(Que* pq)
{
	assert(pq && !QEmpty(pq));

	return pq->tail->data;
}

void QDestroy(Que* pq)
{
	assert(pq);

	while (pq->head)
	{
		QNode* cur = pq->head->next;
		free(pq->head);
		pq->head = cur;
	}

	pq->head = pq->tail = NULL;
}